package com.system.cacheClient.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.concurrent.TimeUnit;

/**
 * 缓存客户端配置类
 */
@Configuration
@EnableConfigurationProperties(CacheClientProperties.class)
@EnableScheduling
public class CacheClientConfig {
    
    /**
     * 配置Caffeine缓存管理器
     * @param cacheClientProperties 缓存客户端配置属性
     * @return CacheManager
     */
    @Bean
    public CacheManager cacheManager(CacheClientProperties cacheClientProperties) {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        if (cacheClientProperties.isLocalCache()) {
            cacheManager.setCaffeine(Caffeine.newBuilder()
                    .maximumSize(cacheClientProperties.getLocalCacheMaxSize())
                    .expireAfterWrite(cacheClientProperties.getLocalCacheExpireAfterWrite(), TimeUnit.MINUTES));
        }
        return cacheManager;
    }
}